// pages/addbaoxiu/addbaoxiu.js

const {addOrder} =require('../../util/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imglist:[],
    imgUrls:"",
    remarks:'',
    heqId:"",
    workImgs:"",
    workContent:'',
 

  },
  shebeibianhao(e){
    
    this.setData({
      heqId:e.detail.value
    })
  },
  input(e){
   
     this.setData({
      workContent:e.detail.value
     })
    

  },
  add(){
    if(this.data.imglist.length >= 5)
    {
      wx.showToast({
        title: '最大上传5张照片',
        icon:'none'
      })
      return
    }
    let that=this
    let imgs = this.data.imglist
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
        imgs.push(res.tempFiles[0])
        that.setData({
          imglist:imgs
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: 'https://anjingbao.anjingbao.net/prod-api/api/common/appUpload',
          success (res){
            console.log(res)
            let jsonS = JSON.parse(res.data)
            that.setData({
              imgUrls:that.data.imgUrls + jsonS.fileName + ","
            })
            
           
            //do something
          }
        })
      }

    })

  },
  orderjilu(){
    wx.navigateTo({
      url: '/pages/shouye/gongdan/gongdan',
    })
  },

  put(){
    
    if(this.data.workContent.length==0){
      wx.showModal({
        title: '请输入报修内容',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.imglist.length==0){
      wx.showModal({
        title: '请上传图片',
        icon: 'none',
        duration: 2000
      })
      return
    }
   
    let p ={
      workImgs:this.data.imgUrls,
       memberId: wx.getStorageSync('memberId'),
       heqId:this.data.heqId,
       workContent:this.data.workContent
      }
      console.log(p)
    addOrder(p).then(res=>{
    console.log(res)
      if(res.code==200){
        wx.showModal({
          title: '报修成功',
          icon: 'success',
          duration: 2000
        })
        }
       else{
         wx.showToast({
           title: res.msg,
           icon: 'error',
           duration: 3000
         })
   
       }
    
    
  })
   wx.navigateBack({
     delta: 1
   })
},
 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    
   
    this.data.heqId=options.heqId
    
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxianginfo.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }

  }
})